package com.b2mining.integration.beans;

import java.util.Vector;


/**
 * Clase que maneja los errores de Base de Datos, parseo y generacion para XML y CSV
 * @since (7/4/2003 1:12:20 PM)
 * @author Synopsis S.A. <BR>
 * Analista Programador: Janyn Roca
 */

public class MapperException extends Exception {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected Vector children = new Vector();
	protected String element;
	protected String error;
	
	public MapperException(){

	}
	public MapperException(String s){
		error = s;
	}
	/**
	 * Returns the element.
	 * @return String
	 */
	public String getElement() {
		return element;
	}
	/**
	 * Sets the element.
	 * @param element The element to set
	 */
	public void setElement(String element) {
		this.element = element;
	}
	/**
	 * Returns the error.
	 * @return String
	 */
	public String getError() {
		return error;
	}

	/**
	 * Sets the error.
	 * @param error The error to set
	 */
	public void setError(String error) {
		this.error = error;
	}
	
	public void addChild(MapperException ex) {
		children.add(ex);
	}
	public void removeChild(int index) {
		children.remove(index);
	}
	public MapperException getChild(int index) {
		return (MapperException) children.get(index);
	}
	public int getChildrenCount() {
		return children.size();
	}

	public String toString() {
		if (error != null)
			return error;
		else {
			String message = "";
			for (int i = 0; i < getChildrenCount(); i++) {
				message = message + getChild(i) + "\n";
			}
			return message;
		}
	}
	
	
	/**
	 * Metodo que crea un bean de process y lo carga con todos los errores encontrados.
	 * 
	 */
	public Process toBean(String tipo, String documentId, String codigoError){
		Process bean = new Process();
		DataError de = null;
		MapperException child = null;
		// Setting the attributes:
		bean.setTipo(tipo);
		bean.setDocumentId(documentId);
		bean.setCodigoError(codigoError.substring(1));
		for (int i=0;i<children.size();i++){
			de = new DataError();
			child = (MapperException) children.get(i);
			de.setCampo(child.getElement());
			de.setError(child.toString());
			bean.addDataError(de);
		}
		return bean;
	}
}
